﻿using DocumentFormat.OpenXml.Bibliography;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using ERP.Team.RBAC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-生产管理-生产报工-xzc
    /// </summary>
    public class ProduceWorkServices: ApplicationService, IProduceWorkServices
    {
        private readonly IRepository<ProductionReportingMain> _productionRepository;
        private readonly IRepository<ProductionWorkOrderMain> _productionWorkOrderMainRepository;
        private readonly IRepository<ProductionTask> _productiontaskRepository;
        private readonly IRepository<DepartmentModel> _departmentRepository;
        private readonly IRepository<UserModel> _userRepository;

        public ProduceWorkServices(IRepository<ProductionReportingMain> productionRepository, IRepository<ProductionWorkOrderMain> productionWorkOrderMainRepository, IRepository<ProductionTask> productiontaskRepository, IRepository<DepartmentModel> departmentRepository, IRepository<UserModel> userRepository)
        {
            _productionRepository = productionRepository;
            _productionWorkOrderMainRepository = productionWorkOrderMainRepository;
            _productiontaskRepository = productiontaskRepository;
            _departmentRepository = departmentRepository;
            _userRepository = userRepository;
        }
        /// <summary>
        /// 查询生产报工
        /// </summary>
        /// <returns></returns>
        public async Task<List<ProductionReportingMain>> GetProductionReportingMain(GetProductionReportingMainDto getproductioreportingmaindto)
        {
            var productionReportingMainList = await _productionRepository.GetListAsync();
            productionReportingMainList = productionReportingMainList.WhereIf(!string.IsNullOrEmpty(getproductioreportingmaindto.ReportingType), m => m.ReportingType == getproductioreportingmaindto.ReportingType).ToList();
            productionReportingMainList = productionReportingMainList.WhereIf(!string.IsNullOrEmpty(getproductioreportingmaindto.WorkstationName), m => m.WorkstationName.Contains(getproductioreportingmaindto.WorkstationName)).ToList();
            productionReportingMainList = productionReportingMainList.WhereIf(!string.IsNullOrEmpty(getproductioreportingmaindto.ProductionWorkOrderCode), m => m.ProductionWorkOrderCode == getproductioreportingmaindto.ProductionWorkOrderCode).ToList();
            productionReportingMainList = productionReportingMainList.WhereIf(!string.IsNullOrEmpty(getproductioreportingmaindto.ProductMaterialCode), m => m.ProductMaterialCode == getproductioreportingmaindto.ProductMaterialCode).ToList();
            productionReportingMainList = productionReportingMainList.WhereIf(!string.IsNullOrEmpty(getproductioreportingmaindto.ProductMaterialName), m => m.ProductMaterialName.Contains(getproductioreportingmaindto.ProductMaterialName)).ToList();
            productionReportingMainList = productionReportingMainList.WhereIf(!string.IsNullOrEmpty(getproductioreportingmaindto.Reporter), m => m.Reporter.Contains(getproductioreportingmaindto.Reporter)).ToList();
            productionReportingMainList = productionReportingMainList.WhereIf(!string.IsNullOrEmpty(getproductioreportingmaindto.Recorder), m => m.Recorder.Contains(getproductioreportingmaindto.Recorder)).ToList();
            productionReportingMainList = productionReportingMainList.WhereIf(getproductioreportingmaindto.States > 0, m => m.States == getproductioreportingmaindto.States).ToList();

            // 添加排序逻辑
            if (!string.IsNullOrEmpty(getproductioreportingmaindto.SortField))
            {
                var isAscending = string.IsNullOrEmpty(getproductioreportingmaindto.SortOrder) || 
                                getproductioreportingmaindto.SortOrder.ToLower() == "asc";

                productionReportingMainList = getproductioreportingmaindto.SortField.ToLower() switch
                {
                    "id" => isAscending 
                        ? productionReportingMainList.OrderBy(x => x.Id).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.Id).ToList(),
                    "reportingtype" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.ReportingType).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.ReportingType).ToList(),
                    "workstationname" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.WorkstationName).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.WorkstationName).ToList(),
                    "productionworkordercode" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.ProductionWorkOrderCode).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.ProductionWorkOrderCode).ToList(),
                    "productmaterialcode" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.ProductMaterialCode).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.ProductMaterialCode).ToList(),
                    "productmaterialname" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.ProductMaterialName).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.ProductMaterialName).ToList(),
                    "reporter" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.Reporter).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.Reporter).ToList(),
                    "recorder" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.Recorder).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.Recorder).ToList(),
                    "states" => isAscending
                        ? productionReportingMainList.OrderBy(x => x.States).ToList()
                        : productionReportingMainList.OrderByDescending(x => x.States).ToList(),
                    _ => productionReportingMainList // 如果没有匹配的排序字段，返回原始列表
                };
            }

            return productionReportingMainList; 
        }
        /// <summary>
        /// 新增生产报工
        /// </summary>
        /// <param name="productionreportingmaindto"></param>
        /// <returns></returns>
        public async Task<int> AddProductionReportingMain(ProductionReportingMainDto productionreportingmaindto)
        {
            var productionreportingmainList = ObjectMapper.Map<ProductionReportingMainDto,ProductionReportingMain>(productionreportingmaindto);
            var i = await _productionRepository.InsertAsync(productionreportingmainList,true);
            return i == null ? 0 : 1;
        }
        /// <summary>
        /// 删除生产报工
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteProductionReportingMain(int id)
        {
            await _productionRepository.DeleteAsync(m => m.Id == id);
        }
        /// <summary>
        /// 批量删除生产报工
        /// </summary>
        /// <param name="ids">要删除的ID数组</param>
        /// <returns>删除成功的数量</returns>
        public async Task<int> BatchDeleteProductionReportingMain(int[] ids)
        {
            if (ids == null || ids.Length == 0)
                return 0;

            var deleteCount = 0;
            foreach (var id in ids)
            {
                try
                {
                    await _productionRepository.DeleteAsync(m => m.Id == id);
                    deleteCount++;
                }
                catch
                {
                    // 继续处理下一个，不中断整个批量删除过程
                    continue;
                }
            }
            return deleteCount;
        }
        /// <summary>
        /// 通过主键id查询到相关的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProductionReportingMain> FindProductionWorkOrderMain(int id)
        {
            return await _productionRepository.GetAsync(m=>m.Id == id);
        }
        /// <summary>
        /// 更新生产报工
        /// </summary>
        /// <param name="productionreportingmaindto"></param>
        /// <returns></returns>
        public async Task<int> UpdateProductionReportingMain(ProductionReportingMainDto productionreportingmaindto)
        {
            var productionreportingmainList = ObjectMapper.Map<ProductionReportingMainDto, ProductionReportingMain>(productionreportingmaindto);
            var i = await _productionRepository.UpdateAsync(productionreportingmainList, true);
            return i == null ? 0 : 1;
        }
        /// <summary>
        /// 查询任务工单的信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<ProductionWorkOrderMain>> GetProductionWorkOrderMain(GetProductionWorkOrderMainDto getproductionworkordermaindto)
        {
            var productionworkordermainList = await _productionWorkOrderMainRepository.GetListAsync();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.WorkOrderCode), m => m.WorkOrderCode == getproductionworkordermaindto.WorkOrderCode).ToList();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.WorkOrderName), m => m.WorkOrderName.Contains(getproductionworkordermaindto.WorkOrderName)).ToList();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.ProductCode), m => m.ProductCode == getproductionworkordermaindto.ProductCode).ToList();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.ProductName), m => m.ProductName.Contains(getproductionworkordermaindto.ProductName)).ToList();
            return productionworkordermainList;
        }
        /// <summary>
        /// 查询生产任务的信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<ProductionTask>> GetProductionTask(string? processBelongs,string? workstationCode,string? taskNumber)
        {
            var productiontaskList = await _productiontaskRepository.GetListAsync();
            productiontaskList = productiontaskList.WhereIf(!string.IsNullOrEmpty(processBelongs), m => m.ProcessBelongs == processBelongs).ToList();
            productiontaskList = productiontaskList.WhereIf(!string.IsNullOrEmpty(workstationCode), m => m.WorkstationCode == workstationCode).ToList();
            productiontaskList = productiontaskList.WhereIf(!string.IsNullOrEmpty(taskNumber), m => m.TaskNumber == taskNumber).ToList();
            return productiontaskList;
        }
        /// <summary>
        /// 查询部门数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<DepartmentModel>> GetDepartment()
        {
            var departmentList = await _departmentRepository.GetListAsync();
            return departmentList;
        }
        /// <summary>
        /// 点击部门查询相对应的用户
        /// </summary>
        /// <param name="departmentid"></param>
        /// <returns></returns>
        public async Task<List<UserModel>> GetUser(int departmentid)
        {
            var userList = await _userRepository.GetListAsync(m=>m.DepartmentId == departmentid);
            return userList;
        }
    }
}
